package com.wc.alorithm_luogu._并查集.程序自动分析;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/1/9 16:26
 * @description https://www.luogu.com.cn/problem/P1955
 */
public class Main {
    /**
     * 思路：
     * 把全部相等的合并到同一个集合当中
     * 检查不相等的是否相等
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010;
    // p1 代表相等的在一起, p2代表不相等的在一起, c是离散化
    static int[] p1 = new int[N], p2 = new int[N], c = new int[N];
    static int[] a = new int[N], b = new int[N], e = new int[N];
    static int n, m;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            for (int i = 1, j = 1; i <= n; i++) {
                c[j++] = a[i] = sc.nextInt();
                c[j++] = b[i] = sc.nextInt();
                e[i] = sc.nextInt();
            }

            Arrays.sort(c, 1, 2 * n + 1);

            m = 1;
            for (int i = 1; i <= 2 * n; i++) {
                if (c[m] != c[i]) c[++m] = c[i];
            }
            for (int i = 1; i <= n; i++) {
                a[i] = get(a[i]);
                b[i] = get(b[i]);
            }

            boolean res = true;
            for (int i = 1; i <= m; i++) p1[i] = p2[i] = i;

            for (int i = 1; i <= n; i++) {
                if (e[i] == 1) {
                    int x = find(a[i], p1), y = find(b[i], p1);
                    p1[x] = y;
                }
            }
            for (int i = 1; i <= n; i++) {
                if (e[i] == 0) {
                    int x = find(a[i], p1), y = find(b[i], p1);
                    if (x == y) {
                        res = false;
                        break;
                    }
                }
            }

            out.println(res ? "YES" : "NO");
        }
        out.flush();
    }

    static int get(int x) {
        int l = 1, r = m;
        while (l < r) {
            int mid = l + r >> 1;
            if (c[mid] >= x) r = mid;
            else l = mid + 1;
        }
        return l;
    }

    static int find(int x, int[] p) {
        if (p[x] != x) p[x] = find(p[x], p);
        return p[x];
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

